package com.mmall.common;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.io.Serializable;

/**
 * @Author 侯俊杰
 * @Date 2017/10/11
 * @Time 20:39
 **/
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
//序列化为json时把value=null的字段取消掉，不添加到json
public class ServerResponse<T> implements Serializable//表示响应里要封装的数据对象是一个什么类型
{
    private int status;
    private String msg;
    private T data;

    private ServerResponse(int status)
    {
        this.status = status;
    }

    private ServerResponse(int status , T data)
    {
        this.status = status;
        this.data = data;
    }

    private ServerResponse(int status ,String msg, T data)
    {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }
    private ServerResponse(int status ,String msg)
    {
        this.status = status;
        this.msg = msg;
    }

    @JsonIgnore
    //序列化之后不显示在json
    public boolean isSuccess()
    {
        return this.status == ResponseCode.SUCCESS.getCode();
    }

    public int getStatus()
    {
        return status;
    }

    public String getMsg()
    {
        return msg;
    }

    public T getData()
    {
        return data;
    }

    public static <T> ServerResponse<T> createBySuccess()
    {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ServerResponse<T> createBySuccessMessage(String msg)
    {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }

    public static <T> ServerResponse<T> createBySuccess(T data)
    {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data);
    }

    public static <T> ServerResponse<T> createBySuccess(String msg,T data)
    {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
    }

    public static <T> ServerResponse<T> createByError()
    {
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
    }

    public static <T> ServerResponse<T> createByErrorMessage(String errorMessage)
    {
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(),errorMessage);
    }

    public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode,String errorMessage)
    {
        return new ServerResponse<T>(errorCode,errorMessage);
    }
}
